﻿using DataCollection.Infrastructure;
using DataCollection.WinForm.ViewModel;
using System.Windows.Forms;
namespace DataCollection.WinForm.Forms
{
    public partial class SystemSetForm : DCBaseForm
    {
        #region 属性
        private SystemSetViewModel viewModel = new SystemSetViewModel(0);

        // <summary>
        /// 需要绑定的控件
        /// </summary>
        private Dictionary<string, Control> ControlDict = new Dictionary<string, Control>();

        public bool StartCollection;

        #endregion

        #region 自定义方法

        #endregion

        #region 系统事件
        public SystemSetForm()
        {
            InitializeComponent();
            foreach (string name in Singleton.Instance.SerialPortList)
            {
                PortAddressSelect.Items.Add(name);
            }
            Header.Text = "系统设置";
            ControlDict.Add("voicePortAddress", PortAddressSelect);
            ControlDict.Add("voiceBaudrate", BaudrateSelect);
            ControlDict.Add("broadcastInterval", BroadcastIntervalInput);
            ControlDict.Add("holdingTime", HoldingTimeInput);
            ControlDict.Add("holdingPointCount", HoldingPointCountInput);
            ControlDict.Add("pressureScaleMultiple", PressureScaleMultipleInput);
            ControlDict.Add("temperatureScaleMultiple", TemperatureScaleMultipleInput);
        }

        private void SystemSetForm_Load(object sender, EventArgs e)
        {
            viewModel = new SystemSetViewModel(1);
            foreach (var item in ControlDict)
            {
                Bindings(item.Value, "Text", item.Key, viewModel);
                AntdUI.IControl control = (AntdUI.IControl)item.Value;
                control.Enabled = !StartCollection;
            }

            Bindings(BroadcastDataCheckbox, "Checked", "broadcastEnable", viewModel); 
            Bindings(VoiceEnableCheckbox, "Checked", "voiceEnable", viewModel);
            VoiceEnableCheckbox.Enabled = !StartCollection;
        }

        #endregion

        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            viewModel.Apply();
            Response response = viewModel.Update();
            if (response.Code == Response.SuccessCode)
            {
                if (this.Owner != null)
                {
                    IndexForm frm = (IndexForm)this.Owner;
                    frm.Success(response.Msg);
                }
                
                this.Close();
            }
            else
            {
                Error(response.Msg);
            }
        }
    }
}
